﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using VBE = Microsoft.Vbe.Interop;

namespace LOVOL_kill_k4_win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "开始清除宏代码\n";

            int codelines = 0;
            int modules = 0;

            Excel.Application ExcelApp;
            try
            {
                ExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            }
            catch (Exception)
            {
                MessageBox.Show("请打开带宏病毒的Excel表格");
                this.textBox1.Text = "请打开带宏病毒的Excel表格,运行本工具时不要关闭EXCEL！\n";
                return;
            }
            

            VBE.VBE vBE;

            try
            {
                vBE = ExcelApp.VBE;
            }
            catch (Exception)
            {
                MessageBox.Show("请打开\"信任对VBA工程对象模型的访问\"功能\n或者，出现“安全警告 宏已被禁用”时点击【启用内容】");
                this.textBox1.Text = "VBA访问权限未开启，请在excel表格【文件】-【选项】-【信任中心】-【信任中心设置】-【宏设置】中开启“信任对VBA工程对象模型的访问”！\n或者，出现“安全警告 宏已被禁用”时点击【启用内容】\n";
                return;
            }

            String vbcs = "";
            this.textBox1.Text = vbcs;

            VBE.VBComponents vBCs;
            
            try
            {
                vBCs = vBE.ActiveVBProject.VBComponents;
            }
            catch (Exception)
            {
                MessageBox.Show("系统中残留Excel进程，请用360加速球加速一下,或打开一个带宏病毒的Excel");
                this.textBox1.Text = "系统中残留Excel进程，请用360加速球加速一下，或打开一个带宏病毒的Excel\n";
                return;
            }

            foreach (VBE.VBComponent vbc in vBCs)
            {

                if (vbc.Type == VBE.vbext_ComponentType.vbext_ct_StdModule)
                {
                    vbcs += "宏模块，" + vbc.Name.ToString() + "删除\n";
                    vBE.ActiveVBProject.VBComponents.Remove(vbc);
                    modules++;
                }
                else if (vbc.Type == VBE.vbext_ComponentType.vbext_ct_Document)
                {
                    if (vbc.Name == "ThisWorkbook")
                    {
                        int lineCount = vbc.CodeModule.CountOfLines;
                        codelines += lineCount;
                        vbcs += vbc.Name.ToString() + "中有" + lineCount.ToString() +"行宏代码，删除\n";
                        vbc.CodeModule.DeleteLines(1, lineCount);
                    }
                }
            }

            this.textBox1.Text = vbcs;
            if (modules == 0 && codelines == 0)
            {
                MessageBox.Show("文件没有包含宏代码！");
            }
            else
            {
                MessageBox.Show("清除成功");
            }
            return;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
